#include "changepersonalinfo.h"
#include "ui_changepersonalinfo.h"
#include "logonwidget.h"
#include "user.h"
#include <QHostAddress>
#include <QMessageBox>
changepersonalinfo::changepersonalinfo(QWidget *parent,user userSelf) :
    QDialog(parent),
    ui(new Ui::changepersonalinfo)
{
    ui->setupUi(this);
    this->userSelf = userSelf;
    connect(ui->btn_cancel,&QPushButton::clicked,this, &changepersonalinfo::on_btn_cancel_clicked);
    connect(ui->btn_change,&QPushButton::clicked,this, &changepersonalinfo::on_btn_change_clicked);
}

changepersonalinfo::~changepersonalinfo()
{
    delete ui;

}
void changepersonalinfo::on_btn_change_clicked()
{
    //get register information
    QString regUsername = ui->reg_username_lineEdit->text();
    QString regPassword = ui->reg_password_lineEdit->text();
    QString regPasswordConfirm = ui->reg_password_conf_lineEdit->text();
    QString senderToken = userSelf.getUserPassword();
    UserChange Sender;
    if(regPassword!=regPasswordConfirm)
    {
        QMessageBox::information(this,"failed","两次输入的密码不一致！");
        this->close();
    }
    Sender = {//新建了一个UserChange的结构体，详情见user.h
        regUsername,//username
        regPassword,
        ""//photo
    };
    QJsonObject new_info_value{
        {"username", Sender.username},
        {"password", Sender.password},
        {"photo", ""}
    };
    //qDebug()<<"SenderValue"<<senderValue;
    QNetworkRequest request(QUrl("http://101.43.168.188:19999/slimarveous/add_friend"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json2");
    //QJsonObject json2;
    QJsonObject json2{//传到后端的json文件
        {"token",senderToken},
        {"id",userSelf.getUserId().toInt()},
        {"new_info",new_info_value},
    };
    QJsonDocument doc(json2);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    QNetworkReply *reply = manager.post(request, data);
    QString message;
    connect(reply, &QNetworkReply::finished, [=]()
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            // 读取响应数据
            QByteArray dataReply = reply->readAll();
            // 将 JSON 数据转换为 QVariant 对象
            QJsonParseError error;
            QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
            //qDebug()<<result;
            // 处理 JSON 数据
            if(error.error == QJsonParseError::NoError)//这个我不知道返回的是什么内容，所以我直接把信息输出了出来
            {
                QVariantMap resultMap = result.toVariantMap();
                //提取状态码
                QVariant status_code = resultMap.value("status_code");
                status_code.toString();
                QString message=resultMap.value("status_msg").toString();
                this->userSelf.setUserName(regUsername);
                if(regPassword!="")
                    this->userSelf.setUserPassword(regPassword);
                QMessageBox:: information(this, "修改成功",message);
            }
        }
        else
            qDebug()<<"error";
        reply->deleteLater();
    });
    //ui->messageSendTextEdit->clear();
    this->close();

}
void changepersonalinfo::on_btn_return_clicked()
{
    this->close();
    LogOnWidget* w1 = new LogOnWidget();
    w1->show();
}
void changepersonalinfo::on_btn_cancel_clicked()
{
    ui->reg_username_lineEdit->clear();
    ui->reg_password_lineEdit->clear();
    ui->reg_password_conf_lineEdit->clear();
}
